//页面打开渲染数据
function commentRender() {
    Ajax({
        url: "../server/commentsRender.php",
        success(msg) {
            sessionStorage.setItem("commentInfo", msg)
            msg = JSON.parse(msg)
            renderFn(msg)

        }
    })
}
commentRender()

//封装渲染函数
function renderFn(data) {
    const tbody = document.getElementById("tbody1")
    tbody.innerHTML = ""
    data.slice(0, 10).forEach(item => {
        let str = `
            <tr>
              <td>${item.Id}</td>
              <td>${item.cname}</td>
              <td>${item.exp}</td>
              <td><button class="btn btn-sm btn-success" style="width: 72px;color:#fff" data-bs-toggle="modal" data-bs-target="#myModal" data-did=${item.Id} data-detail="detail" data-details=${item.detail}>详情</button></td>
              <td><button class="btn btn-sm btn-danger" style="width: 72px;" id=${item.Id} data-del="del" >删除</button></td>
            </tr>
                
                `
        tbody.innerHTML += str
    });
}

//点击删除
const tbody = document.getElementById("tbody1")
tbody.onclick = function (e) {
    e = e || window.event
    if (e.target.dataset.del == "del") {
        const res = confirm("确认删除吗?")
        if (res) {
            Ajax({
                url: "http://localhost/project/server/delComments.php",
                data: { id: e.target.id },
                success() {
                    alert("删除成功!")
                    location.reload()
                }
            })
        }

    }
    if(e.target.dataset.detail == "detail"){
        const details = document.getElementById("details")
        details.innerHTML = e.target.dataset.details
    }
}


//点击首页
function first(){
    sessionStorage.setItem("commentPage",1)
    let commentInfo = sessionStorage.getItem("commentInfo")
    commentInfo = JSON.parse(commentInfo)
    renderFn(commentInfo.slice(0,10))
}
//点击尾页
function last(){
    let commentInfo = sessionStorage.getItem("commentInfo")
    commentInfo = JSON.parse(commentInfo)
    sessionStorage.setItem("commentPage",Math.ceil(commentInfo.length / 10))
    let msg = commentInfo.slice(Math.floor(commentInfo.length / 10) * 10,commentInfo.length)
    renderFn(msg)
}
//点击下一页
function down(){
    let page = +sessionStorage.getItem("commentPage")
    let commentInfo = sessionStorage.getItem("commentInfo")
    commentInfo = JSON.parse(commentInfo)
    page++
    if(page > Math.ceil(commentInfo.length / 10)) page = Math.ceil(commentInfo.length / 10)
    sessionStorage.setItem("commentPage",page)
    let msg = commentInfo.slice((page - 1)* 10,page*10)
    renderFn(msg)
}
//点击上一页
function up(){
    let page = +sessionStorage.getItem("commentPage")
    let commentInfo = sessionStorage.getItem("commentInfo")
    commentInfo = JSON.parse(commentInfo)
    page--
    if(page < 1 ) page = 1
    sessionStorage.setItem("commentPage",page)
    let msg = commentInfo.slice((page - 1) * 10,page*10)
    renderFn(msg)
}

//点击好评
function good(){
    Ajax({
        url:"http://localhost/project/server/goodComments.php",
        success(msg){
    sessionStorage.setItem("commentPage",1)
    sessionStorage.setItem("commentInfo",msg)
    let commentInfo = JSON.parse(msg)
    commentInfo = commentInfo.slice(0,10)
    renderFn(commentInfo)
        }
    })
}
//点击差评
function bad(){
    Ajax({
        url:"http://localhost/project/server/badComments.php",
        success(msg){
    sessionStorage.setItem("commentPage",1)
    sessionStorage.setItem("commentInfo",msg)
    let commentInfo = JSON.parse(msg)
    commentInfo = commentInfo.slice(0,10)
    renderFn(commentInfo)
        }
    })
}